package com.pkx.guide.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pkx.guide.common.util.StringUtils;
import com.pkx.guide.system.entity.TSysOperLog;
import com.pkx.guide.system.mapper.TSysOperLogMapper;
import com.pkx.guide.system.service.TSysOperLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 操作日志记录 服务实现类
 *
 * @author sunsy
 * @since 2022-01-21
 */
@Service
public class TSysOperLogServiceImpl extends ServiceImpl<TSysOperLogMapper, TSysOperLog> implements TSysOperLogService {

    /**
     * 分页列表
     *
     * @param tSysOperLog
     * @return
     */
    @Override
    public IPage<TSysOperLog> pageList(Page page, TSysOperLog tSysOperLog) {
        LambdaQueryWrapper<TSysOperLog> lambdaQueryWrapper = new QueryWrapper<TSysOperLog>().lambda();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(tSysOperLog.getTitle()), TSysOperLog::getTitle, tSysOperLog.getTitle());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(tSysOperLog.getMethod()), TSysOperLog::getMethod, tSysOperLog.getMethod());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(tSysOperLog.getRequestMethod()), TSysOperLog::getRequestMethod, tSysOperLog.getRequestMethod());
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(tSysOperLog.getOperName()), TSysOperLog::getOperName, tSysOperLog.getOperName());
        lambdaQueryWrapper.eq(tSysOperLog.getBusinessType() != null, TSysOperLog::getBusinessType, tSysOperLog.getBusinessType());
        lambdaQueryWrapper.eq(tSysOperLog.getStatus() != null, TSysOperLog::getStatus, tSysOperLog.getStatus());
        lambdaQueryWrapper.apply(StringUtils.isNotEmpty(tSysOperLog.getQueryStartDate()), "date_format(create_time,'%Y-%m-%d') >= '" + tSysOperLog.getQueryStartDate() + "'");
        lambdaQueryWrapper.apply(StringUtils.isNotEmpty(tSysOperLog.getQueryEndDate()), "date_format(create_time,'%Y-%m-%d') <= '" + tSysOperLog.getQueryEndDate() + "'");
        lambdaQueryWrapper.eq(TSysOperLog::getDelFlg, 0);
        lambdaQueryWrapper.orderByDesc(TSysOperLog::getCreateTime);
        return page(page, lambdaQueryWrapper);
    }

    /**
     * 列表查询
     *
     * @param tSysOperLog
     * @return
     */
    @Override
    public List<TSysOperLog> listByEntity(TSysOperLog tSysOperLog) {
        LambdaQueryWrapper<TSysOperLog> lambdaQueryWrapper = new QueryWrapper<TSysOperLog>().lambda();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(tSysOperLog.getTitle()), TSysOperLog::getTitle, tSysOperLog.getTitle());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(tSysOperLog.getMethod()), TSysOperLog::getMethod, tSysOperLog.getMethod());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(tSysOperLog.getRequestMethod()), TSysOperLog::getRequestMethod, tSysOperLog.getRequestMethod());
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(tSysOperLog.getOperName()), TSysOperLog::getOperName, tSysOperLog.getOperName());
        lambdaQueryWrapper.eq(tSysOperLog.getBusinessType() != null, TSysOperLog::getBusinessType, tSysOperLog.getBusinessType());
        lambdaQueryWrapper.eq(tSysOperLog.getStatus() != null, TSysOperLog::getStatus, tSysOperLog.getStatus());
        lambdaQueryWrapper.apply(StringUtils.isNotEmpty(tSysOperLog.getQueryStartDate()), "date_format(create_time,'%Y-%m-%d') >= '" + tSysOperLog.getQueryStartDate() + "'");
        lambdaQueryWrapper.apply(StringUtils.isNotEmpty(tSysOperLog.getQueryEndDate()), "date_format(create_time,'%Y-%m-%d') <= '" + tSysOperLog.getQueryEndDate() + "'");
        lambdaQueryWrapper.eq(TSysOperLog::getDelFlg, 0);
        lambdaQueryWrapper.orderByDesc(TSysOperLog::getCreateTime);
        return list(lambdaQueryWrapper);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public TSysOperLog selectById(Integer id) {
        return getById(id);
    }

    /**
     * 保存
     *
     * @param tSysOperLog
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveTSysOperLog(TSysOperLog tSysOperLog) {
        Boolean bol = save(tSysOperLog);
        return bol;
    }

    /**
     * 修改
     *
     * @param tSysOperLog
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean modify(TSysOperLog tSysOperLog) {
        return updateById(tSysOperLog);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Integer id) {
        if (id == null){
            return false;
        }
        LambdaUpdateWrapper<TSysOperLog> lambdaUpdateWrapper = new UpdateWrapper<TSysOperLog>().lambda()
                .eq(TSysOperLog::getOperId, id)
                .set(TSysOperLog::getDelFlg, 1);
        return update(lambdaUpdateWrapper);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchDeleteByIds(String ids){
        if (StringUtils.isEmpty(ids)){
            throw new IllegalArgumentException("参数为空");
        }
        String[] idArr = ids.split(",");
        if (idArr.length > 0){
            for (int i = 0; i < idArr.length; i++){
                deleteById(Integer.valueOf(idArr[i]));
            }
        }
        return true;
    }
}
